package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TdActivity
import client.comm.baoding.ui.TdSubActivity
import client.comm.baoding.ui.TdlstjActivity
import client.comm.baoding.ui.TuiGuangActivity
import client.comm.commlib.base.BasePagingAdapter
import client.comm.commlib.base.PagingViewHoder
import client.comm.commlib.utils.StoreUtil

import com.bumptech.glide.RequestManager
import com.google.gson.Gson

class Td2Adapter(val context: Context, val manager: RequestManager) :
    BasePagingAdapter<TdBean.Sub>(object : DiffUtil.ItemCallback<TdBean.Sub>() {
        val g = Gson()

        override fun areItemsTheSame(
            oldItem: TdBean.Sub,
            newItem: TdBean.Sub
        ): Boolean {
            return true
        }

        override fun areContentsTheSame(
            oldItem: TdBean.Sub,
            newItem: TdBean.Sub
        ): Boolean {
            return g.toJson(oldItem).equals(g.toJson(newItem))
        }
    }) {

    lateinit var tdBean1: TdBean
    var level = 0

    override fun getItemViewType(position: Int): Int {
        if (position == 0) return 1
        return super.getItemViewType(position)
    }

    fun onItemClick(b: TdBean.Sub) {
        if (level < 2)
            context.startActivity(Intent(context, TdActivity::class.java).apply {
                putExtra("bean", b.id)
                putExtra("level", level)
                putExtra("name", b.nickname)

            })
    }

    /**
     * 历史推荐
     */
    open fun lstj() {
//        startActivity(Intent(context, TdActivity::class.java).apply {
//            putExtra("bean", StoreUtil.getBean<UserInfo>(requireContext())!!.id)
//        })

        context.startActivity(Intent(context, TdlstjActivity::class.java)
            .apply {
                putExtra("bean", StoreUtil.getBean<UserInfo>(context)!!.id)
            }
        )
    }

    override fun bindItem(holder: PagingViewHoder, t: Any) {
        val binding = holder.binding as LayoutTdnewitemBinding
        val bean = t as TdBean.Sub
        manager.load(bean.avatar).error(R.mipmap.ic_launcher_round).into(binding.img)
        binding.bean = bean
        binding.event = this
    }


    override fun getItemLayout(): Int {
        return R.layout.layout_tdnewitem
    }

}